<?php
    class Menu {
        /**
         * Palauttaa käyttöliittymän menun.
         *
         * @param   bool    $visible    True = Palauta vain näytettävät
         * @return  array
         */
        public static function getMenu($visible = true) {
            $menu = array();
            
            # True  = view
            # False = action
            # Null  = ei näytetä
            $menu["main"]["main"]               = true;
            
            $menu["workout"]["addworkout"]      = true;
            $menu["workout"]["editworkout"]     = null;
            $menu["workout"]["list"]            = true;
            $menu["workout"]["onerepmax"]       = true;
            $menu["workout"]["trainingvolume"]  = true;
            
            $menu["tp"]["addtrainingprogram"]   = true;
            $menu["tp"]["edittrainingprogram"]  = null;
            $menu["tp"]["tpcalendar"]           = true;
            $menu["tp"]["tplist"]               = true;
            
            $menu["template"]["addtemplate"]    = true;
            $menu["template"]["edittemplate"]   = null;
            $menu["template"]["templatelist"]   = true;
            
            $menu["user"]["account"]            = true;
            $menu["user"]["userweight"]         = true;
            
            $menu["logout"]["logout"]           = false;
            
            # Poimi halutut
            $result = array();
            foreach ($menu as $root => $submenu) {
                $arr = array();
                foreach ($submenu as $subview => $isView) {
                    if (!isset($isView) && !$visible) {
                        $arr[$subview] = $isView;
                    } else if (isset($isView)) {
                        $arr[$subview] = $isView;
                    }
                }
                if (!empty($arr)) {
                    $result[$root] = $arr;
                }
            }
            
            return $result;
        }
        
        /**
         * Palauttaa menun selväkielisen nimen.
         *
         * @param   string  $view   Menun tekninen nimi
         * @return  mixed
         */
        public static function getMenuTitle($menu) {
            $result = false;
            
            $titles = array(
                "main"      => "Main",
                "workout"   => "Workouts",
                "tp"        => "Training programs",
                "template"  => "Templates",
                "user"      => "User",
                "logout"    => "Logout"
            );
            
            if (isset($titles[$menu])) {
                $result = $titles[$menu];
            }
            
            return $result;
        }
        
        /**
         * Palauttaa menun teknisen nimen johon näkymä kuuluu.
         *
         * @param   string  $view   Näkymän tekninen nimi
         * @return  mixed
         */
        public static function getViewMenu($view) {
            $result = false;
            
            foreach (self::getMenu(false) as $root => $submenu) {
                foreach ($submenu as $subview => $isView) {
                    if ($view == $subview) {
                        $result = $root;
                        break;
                    }
                }
            }
            
            return $result;
        }
        
        /**
         * Palauttaa menun näkymän selväkielisen nimen.
         *
         * @param   string  $view   Näkymän tekninen nimi
         * @return  mixed
         */
        public static function getViewTitle($view) {
            $result = false;
            
            $titles = array(
                "main"                  => "Main",
                "addworkout"            => "Add",
                "editworkout"           => "Edit",
                "list"                  => "List",
                "onerepmax"             => "1RM progression",
                "trainingvolume"        => "Training volume",
                "addtrainingprogram"    => "Add",
                "edittrainingprogram"   => "Edit",
                "tpcalendar"            => "Calendar",
                "tplist"                => "List",
                "addtemplate"           => "Add",
                "edittemplate"          => "Edit",
                "templatelist"          => "List",
                "account"               => "Account",
                "userweight"            => "Weight & fat percentage",
                "logout"                => "Logout"
            );
            
            if (isset($titles[$view])) {
                $result = $titles[$view];
            }
            
            return $result;
        }
    }